package com.lngsyz.ywtb.biz.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lngsyz.ywtb.biz.service.NotLoginService;
import com.lngsyz.ywtb.entity.CommonConstant;
import com.lngsyz.ywtb.entity.URIResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
@Api(tags = "NotLoginController", description = "不需要登录")
@RequestMapping("v1/notLogin")
public class NotLoginController {

	@Autowired()
	private NotLoginService notLoginService;

	@ApiOperation(value = "获取访问地址", notes = "公众号打开页面，获取获取访问地址，返回页面url")
	@RequestMapping(value = "/getUrl", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<String> getUrl(@ApiParam(name = "id", value = "用户获取当前单位的id") @RequestParam(value = "id", defaultValue = "") String id,
			@ApiParam(name = "appId", value = "应用id") @RequestParam(value = "appId", defaultValue = "") String appId,
			@ApiParam(name = "appSecret", value = "授权密钥") @RequestParam(value = "appSecret", defaultValue = "") String appSecret,
			HttpServletRequest request) {
		URIResult<String> res = new URIResult<String>();
		try {
			String tokenStr = notLoginService.getUrl(appId, appSecret, id);
			res.setData(tokenStr);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "获取真实token", notes = "通过url后缀的tokenId获取真实token")
	@RequestMapping(value = "/getToken", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<String> getToken(@ApiParam(name = "id", value = "url后缀的id") @RequestParam(value = "id", defaultValue = "") String id,
			HttpServletRequest request) {
		URIResult<String> res = new URIResult<String>();
		try {
			String tokenStr = notLoginService.getToken(id);
			res.setData(tokenStr);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
}